<?php

require_once 'Basic.php';

class FormFieldAbsNumber extends FormFieldType {

  private $minValue;
  private $maxValue;

  public function __construct($minValue = null, $maxValue = null) {
    $this->htmlId = null;
    $this->size = 20;
    $this->htmlClass = "priceField";
    $this->minValue = $minValue;
    $this->maxValue = $maxValue;
  }
  
  public function getHtml($name, $value = null) {
    return $this->commonHtml($name,$value);
  }
  
  public function validate($input) {
    if(preg_match("%^[0-9]+$%", $input) === 1) {
      $number = intval($input);
      if(($this->minValue === null || $number >= $this->minValue) && ($this->maxValue === null || $number <= $this->maxValue)){
        return true;
      }
      else {
        return "O n&uacute;mero deve estar entre " . $this->minValue . " e " . $this->maxValue .
               ". Foi dado <span class='userInput'>${number}</span>.";
      }
    }
    else {
      return "O n&uacute;mero deve conter digitos de 0 a 9. Foi dado " . 
             "<span class='userInput'>${input}</span>";
    }
  }
  
  public function clearHtmlClass() {
    $this->htmlClass = "textlineField";
  }

}